<?php
namespace app\index\controller\system;

use app\index\controller\Base;
use think\Cache;
class CodeMap extends Base
{
    public $tableName = "CodeMap";
    public function index()
    {
        $this->page([],10,$this->tableName,"id desc","","group_name","id,group_name,title,create_time");
        return view();
    }

    /**
     *  添加
     */

     public  function info(){
         $type=input("type");
        $this->page(["group_name"=>$type],10,$this->tableName,"id desc","");
        return view();
     }
    public function add()
    {
        $model = model('CodeMap');
        if (request()->instance()->isPost()) {
            $post = request()->instance()->param();        
            $data["title"] = $post["title"];
            $data["group_name"] = $post["group_name"];        
            $data["m_value"] = $post["m_value"];
            $data["m_name"] = $post["m_name"];
            $data["sort"] = $post["sort"];

            $CodeMapModel = model("CodeMap");
           $count= $CodeMapModel->where(["m_value"=> $post["m_value"],"group_name"=>$post["group_name"]])->count();
            if($count>0){
                $this->error("键值不能重复");
            }
            $result = $CodeMapModel->save($data);
            if (empty($result)) {
                $this->error("操作失败");

            }
            $code_map_list = $CodeMapModel->all()->toArray();
            Cache::set("code_map", serialize($code_map_list));
            $this->success("操作成功", url("/Index/System/CodeMap/info",["type"=>$post["group_name"]]));
        }

        return view();
    }

    /**
     *  修改
     */
    public function edit()
    {
        $CodeMapModel = model('CodeMap');
        if (request()->instance()->isPost()) {
            $post = request()->instance()->param();
          
            $data["title"] = $post["title"];
            $data["group_name"] = $post["group_name"];
            $data["m_name"] = $post["m_name"];
            $data["m_value"] = $post["m_value"];
            $data["sort"] = $post["sort"];
            $map["id"] = $post["id"];
            $count= $CodeMapModel->where(["m_value"=> $post["m_value"],"group_name"=>$post["group_name"],"id"=>array("NEQ",intval($post["id"]))])->count();
            if($count>0){
                $this->error("键值不能重复");
            }
            $result = $CodeMapModel->save($data, $map);
            if (empty($result)) {
                $this->error("操作失败");

            }
            $code_map_list = $CodeMapModel->all()->toArray();
            Cache::set("code_map", serialize($code_map_list));
            $this->success("操作成功", url("/Index/System/CodeMap/info",["type"=>$post["group_name"]]));
        }

        $id = input("id");

        $data["info"] = $CodeMapModel->get($id);
       
        $this->assign($data);
        return view();
    }

    public function del()
    {
        $id = input("id");
        if (empty($id)) {
            $this->error("删除失败");
        }
        $CodeMapModel = model('CodeMap');
        $result = $CodeMapModel->where('id', intval($id))->delete();
        if ($result) {
            $code_map_list = $CodeMapModel->all()->toArray();
            Cache::set("code_map", serialize($code_map_list));
            $this->success("删除成功");
        }
        $this->error("删除失败");
    }

}
